package com.leesche.zcsp.node.web.controller.web.operation;

import com.leesche.zcsp.node.utils.web.PagingResult;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.service.PubDeviceDiscountService;
import com.leesche.zcsp.node.web.service.PubDiscountService;
import com.leesche.zcsp.node.web.service.impl.data.read.PubDeviceDiscountDto;
import com.leesche.zcsp.node.web.service.impl.data.read.PubDiscountDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDeviceDiscountCondition;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDiscountCondition;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by kaider on 2016/10/5.
 */
@Controller
@RequestMapping("/web/deviceDiscount")
public class DeviceDiscountController
{
    private final static Logger LOGGER = LoggerFactory.getLogger(DeviceDiscountController.class);

    private static final String LIST_VIEW = "web/deviceDiscount/listView";
    private static final String ADD_VIEW = "web/deviceDiscount/addView";

    @Autowired
    private PubDeviceDiscountService deviceDiscountService;

    @Autowired
    private PubDiscountService discountService;

    @Autowired
    private PrivilegeService privilegeService;

    @RequestMapping(value = "/listView", method = RequestMethod.GET)
    public String listView()
    {
        return LIST_VIEW;
    }


    @ResponseBody
    @RequestMapping(value = "/list.json", method = RequestMethod.GET)
    public Map<String, Object> listJson(
            final QueryPubDeviceDiscountCondition condition
    )
    {
        condition.setAdminId(privilegeService.getCurrentPrincipal().getUserid());
        PagingResult<PubDeviceDiscountDto> pagingResult = deviceDiscountService.findAllByPage(condition);
        Map<String, Object> json = WebResult.buildOf(pagingResult).toMap();
        return json;
    }


    @RequestMapping(value = "/addView", method = RequestMethod.GET)
    public String addView(
            final Model model
    )
    {
        QueryPubDiscountCondition condition = new QueryPubDiscountCondition();
        condition.setAdminId(privilegeService.getCurrentPrincipal().getUserid());
        List<PubDiscountDto> list = discountService.findDiscountList(condition);
        model.addAttribute("list", list);
        return ADD_VIEW;
    }

}
